﻿package mortal.game.scene3D.layer3D.utils
{
    import Message.BroadCast.*;
    import Message.DB.Tables.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import frEngine.event.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.rules.*;
    import mortal.game.scene3D.events.*;
    import mortal.game.scene3D.layer3D.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.mvc.core.*;

    public class EntityUtil extends EntityLayerUtil
    {
        private var _entitysMap:EntityDictionary;
        private var _isWizardShow:Boolean = true;
        protected var _hideBossCodeArray:Vector.<int>;
        public static const LinkStr:String = "_";

        public function EntityUtil(param1:PlayerLayer)
        {
            this._hideBossCodeArray = new Vector.<int>;
            super(param1);
            this._entitysMap = new EntityDictionary();
            return;
        }// end function

        public function isAttackAble(param1:IEntity, param2:Boolean = true) : Boolean
        {
            if (param1)
            {
                return true;
            }
            return false;
        }// end function

        public function addPlayers(param1:SEntityId, param2:SpritePlayer) : void
        {
            this._entitysMap.addEntity(param1, param2);
            return;
        }// end function

        public function getEntity(param1:SEntityId) : IEntity
        {
            return this._entitysMap.getEntity(param1);
        }// end function

        public function getSelfPetEntity() : PetPlayer
        {
            var _loc_1:* = Cache.instance.role.roleEntityInfo.entityInfo.entityId;
            var _loc_2:* = new SEntityId();
            _loc_2.id = _loc_1.id;
            _loc_2.type = EEntityType._EEntityTypePet;
            _loc_2.typeEx = _loc_1.typeEx;
            _loc_2.typeEx2 = _loc_1.typeEx2;
            return this.getEntity(_loc_2) as PetPlayer;
        }// end function

        public function getSelfEscortEntity() : IEntity
        {
            var _loc_1:* = Cache.instance.role.roleEntityInfo.entityInfo.entityId;
            var _loc_2:* = new SEntityId();
            _loc_2.id = _loc_1.id;
            _loc_2.type = EEntityType._EEntityTypeEscort;
            _loc_2.typeEx = _loc_1.typeEx;
            _loc_2.typeEx2 = _loc_1.typeEx2;
            return this.getEntity(_loc_2);
        }// end function

        public function moveEntity(param1:SEntityMoveInfo, param2:Array) : Boolean
        {
            var _loc_3:SpritePlayer = null;
            if (param1.msgEx)
            {
                _loc_3 = ThingUtil.embedSceneEntityUtil.getEntity(param1.entityId, param1.msgEx.ex1) as SpritePlayer;
            }
            else
            {
                _loc_3 = this._entitysMap.getEntity(param1.entityId) as SpritePlayer;
            }
            if (_loc_3)
            {
                _loc_3.walking(param2);
                ThingUtil.isEntitySort = true;
                return true;
            }
            return false;
        }// end function

        public function diversionEntity(param1:SEntityMoveInfo, param2:Array) : Boolean
        {
            var _loc_3:SpritePlayer = null;
            if (param1.msgEx)
            {
                _loc_3 = ThingUtil.embedSceneEntityUtil.getEntity(param1.entityId, param1.msgEx.ex1) as SpritePlayer;
            }
            else
            {
                _loc_3 = this._entitysMap.getEntity(param1.entityId) as SpritePlayer;
            }
            if (_loc_3)
            {
                _loc_3.diversion(param2);
                ThingUtil.isEntitySort = true;
                return true;
            }
            return false;
        }// end function

        public function createEntity(param1:EntityInfo) : void
        {
            var _loc_3:SpritePlayer = null;
            var _loc_4:SpritePlayer = null;
            var _loc_2:* = param1.entityInfo;
            if (_loc_2.msgEx == null)
            {
                _loc_3 = this._entitysMap.getEntity(_loc_2.entityId) as SpritePlayer;
                if (_loc_3 == null)
                {
                    _loc_3 = EntityClass.getInstanceByType(_loc_2.entityId.type) as SpritePlayer;
                    this._entitysMap.addEntity(_loc_2.entityId, _loc_3);
                }
                _loc_3.updateInfo(param1, true);
                ThingUtil.isEntitySort = true;
                Dispatcher.dispatchEvent(new DataEvent(EventName.Scene_AddEntity, _loc_2));
                if (EntityRelationUtil.isSelfPetByEntityId(_loc_2.entityId))
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Scene_AddEntityPet, _loc_3));
                }
                if (_loc_3 is UserPlayer)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Scene_AddEntityUser, _loc_3));
                }
            }
            else
            {
                _loc_2.entityId.msgEx = _loc_2.msgEx;
                _loc_4 = ThingUtil.embedSceneEntityUtil.getEntity(_loc_2.entityId, _loc_2.msgEx.ex1) as SpritePlayer;
                if (_loc_4 == null)
                {
                    _loc_4 = EntityClass.getInstanceByType(_loc_2.entityId.type) as SpritePlayer;
                    ThingUtil.embedSceneEntityUtil.addEntity(_loc_2.entityId, _loc_4, _loc_2.msgEx.ex1);
                }
                _loc_4.updateInfo(_loc_2, true);
            }
            return;
        }// end function

        public function removeEntitys(param1:Array) : void
        {
            var _loc_3:SEntityId = null;
            var _loc_2:* = param1.length;
            for each (_loc_3 in param1)
            {
                
                this.removeEntity(_loc_3);
            }
            return;
        }// end function

        public function removeEntity(param1:SEntityId) : void
        {
            var _loc_2:SpritePlayer = null;
            var _loc_3:EntityInfo = null;
            var _loc_4:SpritePlayer = null;
            if (param1 == null)
            {
                return;
            }
            if (param1.msgEx == null)
            {
                if (param1.type == EntityType.DropItem)
                {
                    ThingUtil.dropItemUtil.removeDropItem(param1);
                    return;
                }
                _loc_2 = this._entitysMap.getEntity(param1) as SpritePlayer;
                if (_loc_2 == null)
                {
                    Cache.instance.entity.removeEntity(param1);
                    return;
                }
                _loc_3 = Cache.instance.entity.getEntityInfoById(param1);
                if (_loc_2 is MonsterPlayer && _loc_2.entityInfo && _loc_2.entityInfo.entityInfo.life == 0)
                {
                    (_loc_2 as MonsterPlayer).death();
                }
                else
                {
                    FrEventDispatcher.instance.proxyDispatchEvent(_loc_2, PlayerEvent.ENTITY_DEAD, [_loc_2]);
                    _loc_2.dispose();
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.Scene_RemoveEntity, param1));
            }
            else
            {
                _loc_4 = ThingUtil.embedSceneEntityUtil.getEntity(param1, param1.msgEx.ex1) as SpritePlayer;
                if (_loc_4 == null)
                {
                    return;
                }
                if (_loc_4.life > 0)
                {
                    ThingUtil.embedSceneEntityUtil.deleteEntity(param1);
                    _loc_4.dispose();
                }
                else
                {
                    _loc_4.death();
                }
            }
            return;
        }// end function

        override public function removeAll() : void
        {
            var _loc_1:Pivot3D = null;
            for each (_loc_1 in this._entitysMap.allEntitys)
            {
                
                if (_loc_1)
                {
                    _loc_1.dispose();
                }
            }
            this._entitysMap.removeAll();
            return;
        }// end function

        public function get entitysMap() : EntityDictionary
        {
            return this._entitysMap;
        }// end function

        public function set entitysMap(param1:EntityDictionary) : void
        {
            this._entitysMap = param1;
            return;
        }// end function

        public function refreshPoint(param1:SEntityId, param2:Array) : void
        {
            var _loc_4:SPoint = null;
            var _loc_3:* = this._entitysMap.getEntity(param1) as SpritePlayer;
            if (_loc_3)
            {
                _loc_4 = param2[(param2.length - 1)] as SPoint;
                if (_loc_3 is MovePlayer)
                {
                    (_loc_3 as MovePlayer).stopMove();
                }
                _loc_3.setPixlePoint(_loc_4.x, _loc_4.y);
                ThingUtil.isMoveChange = true;
            }
            return;
        }// end function

        public function updateBossIsAddToStage(param1:int, param2:Boolean) : void
        {
            var _loc_3:* = this._hideBossCodeArray.indexOf(param1);
            if (param2 && _loc_3 < 0)
            {
                this._hideBossCodeArray.push(param1);
            }
            if (!param2 && _loc_3 >= 0)
            {
                this._hideBossCodeArray.slice(_loc_3, 1);
            }
            return;
        }// end function

        public function isBossHide(param1:int) : Boolean
        {
            return this._hideBossCodeArray.indexOf(param1) >= 0;
        }// end function

        public function updateHeadContainer() : void
        {
            this._entitysMap.updateHeadContainer();
            return;
        }// end function

        public function get isWizardShow() : Boolean
        {
            return this._isWizardShow;
        }// end function

        public function set isWizardShow(param1:Boolean) : void
        {
            var _loc_2:IEntity = null;
            if (this._isWizardShow != param1)
            {
                this._isWizardShow = param1;
                for each (_loc_2 in this._entitysMap.allEntitys)
                {
                    
                    if (_loc_2 is UserPlayer)
                    {
                        (_loc_2 as UserPlayer).updateWizardVisible();
                    }
                }
            }
            return;
        }// end function

        public function get danceInfo() : Array
        {
            var _loc_2:SpritePlayer = null;
            var _loc_1:* = RolePlayer.instance.entityInfo.entityInfo.entityId.id;
            for each (_loc_2 in this.entitysMap.allEntitys)
            {
                
                if (_loc_2 is UserPlayer && _loc_2.entityInfo.isInDance && _loc_2.actionName == ActionName.Dance && _loc_2.entityInfo.entityInfo.entityId.id != _loc_1)
                {
                    return UserPlayer(_loc_2).danceInfo;
                }
            }
            return null;
        }// end function

        public static function toString(param1:SEntityId) : String
        {
            return param1.type.toString() + LinkStr + param1.typeEx.toString() + LinkStr + param1.typeEx2.toString() + LinkStr + param1.id.toString();
        }// end function

        public static function equal(param1:SEntityId, param2:SEntityId) : Boolean
        {
            if (!param1 || !param2)
            {
                return false;
            }
            return param1.id == param2.id && param1.type == param2.type && param1.typeEx == param2.typeEx && param1.typeEx2 == param2.typeEx2;
        }// end function

        public static function isSelf(param1:SEntityId) : Boolean
        {
            return equal(param1, Cache.instance.role.entityInfo.entityId);
        }// end function

        public static function isSelfProxy(param1:SEntityId) : Boolean
        {
            return isSameProxy(param1, Cache.instance.role.entityInfo.entityId);
        }// end function

        public static function petEqual(param1:SEntityId, param2:SEntityId) : Boolean
        {
            return param1.id == param2.id && param1.typeEx == param2.typeEx && param1.typeEx2 == param2.typeEx2;
        }// end function

        public static function isOwerEntity(param1:SEntityInfo, param2:SEntityId) : Boolean
        {
            return false;
        }// end function

        public static function isSameProxy(param1:SEntityId, param2:SEntityId) : Boolean
        {
            return param1.typeEx2 == param2.typeEx2;
        }// end function

        public static function isSameServer(param1:SEntityId, param2:SEntityId) : Boolean
        {
            if (!param1 || !param2)
            {
                return false;
            }
            return param1.typeEx2 == param2.typeEx2 && param1.typeEx == param2.typeEx;
        }// end function

        public static function isSameProxyByRole(param1:SEntityId) : Boolean
        {
            return Cache.instance.role.entityInfo.entityId.typeEx2 == param1.typeEx2;
        }// end function

        public static function isSameServerByRole(param1:SEntityId) : Boolean
        {
            var _loc_2:* = Cache.instance.role.entityInfo.entityId;
            return isSameServer(_loc_2, param1);
        }// end function

        public static function isSameServerByProxyAndServer(param1:int, param2:int) : Boolean
        {
            var _loc_3:* = Cache.instance.role.entityInfo.entityId;
            return _loc_3.typeEx2 == param1 && _loc_3.typeEx == param2;
        }// end function

        public static function isSameProxyByRoleProxyId(param1:int) : Boolean
        {
            return Cache.instance.role.entityInfo.entityId.typeEx2 == param1;
        }// end function

        public static function isNullEntity(param1:SEntityId) : Boolean
        {
            if (param1 == null)
            {
                return true;
            }
            if (param1.id == 0 && param1.type == 0 && param1.typeEx == 0 && param1.typeEx2 == 0)
            {
                return true;
            }
            return false;
        }// end function

        public static function isMercenary(param1:SEntityId) : Boolean
        {
            return param1.type == EEntityType._EEntityTypeMercenary;
        }// end function

        public static function getBossEntityInfoArr() : Array
        {
            var _loc_3:EntityInfo = null;
            var _loc_4:TBoss = null;
            var _loc_1:Array = [];
            var _loc_2:* = Cache.instance.entity.getAllEntityInfo(EEntityType._EEntityTypeBoss);
            for each (_loc_3 in _loc_2)
            {
                
                if (_loc_3)
                {
                    _loc_4 = BossConfig.instance.getInfoByCode(_loc_3.entityInfo.code);
                    if (_loc_4 && _loc_4.category == EBossCategory._EBossCategoryBoss)
                    {
                        _loc_1.push(_loc_3);
                    }
                }
            }
            return _loc_1;
        }// end function

    }
}
